﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Report;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

namespace SIPO.Web.lists.Report
{
    public partial class rptBillinglist : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                recBillingRepo recBillingDal = new recBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recTransactionTypeRepo recTransactionTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                rptRecBillingRepo rptRecBillingDal = new rptRecBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                string parameters = string.Empty;

                //populate dropdownlist transactionType
                List<recTransactionType> transTypes = recTransactionTypeDal.GetList();

                ddlJenisTagihan.Items.Clear();
                if (transTypes.Count > 0)
                {
                    transTypes.ForEach(delegate(recTransactionType transType)
                    {
                        ddlJenisTagihan.Items.Add(new ListItem(transType.TransTypeCode + "-" + transType.TransTypeName, transType.TransTypeID.ToString()));
                    });
                }
                ddlJenisTagihan.Items.Insert(0, new ListItem("-- jenis tagihan --", string.Empty));

                //populate dropdownlist aksi debitur
                //txtDueDateAwal.Text = base.UserID.ToString();
                DataTable dtDebtor = new DataTable();
                dtDebtor = rptRecBillingDal.getDataAksiDebitur(base.UserID);
                dtDebtor.Columns.Add("FullName", typeof(string), "DebtorSectorID + '-' + DebtorName + '-' + SectorName + '-' + SubSectorName + '-' + SubSubSectorName");

                if (dtDebtor.Columns.Count > 0)
                {
                    ddlAksiDebitur.DataSource = dtDebtor;
                    ddlAksiDebitur.DataTextField = "FullName";
                    ddlAksiDebitur.DataValueField = "DebtorSectorID";
                    ddlAksiDebitur.DataBind();
                }

                ddlAksiDebitur.Items.Insert(0, new ListItem("-- aksi debitur --", string.Empty));

                if (Context.Request.Params["periodeAwal"] != null || Context.Request.Params["periodeAkhir"] != null || Context.Request.Params["dueDateAwal"] != null)
                {
                    DateTime? sPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
                    DateTime? sPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
                    DateTime? sDueDateAwal = Context.Request.Params["dueDateAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["DueDateAwal"], Helper.Culture.Indonesian);
                    DateTime? sDueDateAkhir = Context.Request.Params["dueDateAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["DueDateAkhir"], Helper.Culture.Indonesian);
                    string transTypeID = Context.Request.Params["transTypeID"] == null ? string.Empty : Context.Request.Params["transTypeID"];
                    string debtorSectorID = Context.Request.Params["debtorSectorID"] == null ? string.Empty : Context.Request.Params["debtorSectorID"];
                    string balance = Context.Request.Params["balance"] == null ? string.Empty : Context.Request.Params["balance"];


                    //konversi tanggal
                    string periodeAwal = string.Format("{0:yyyyMMdd}", sPeriodeAwal);
                    string periodeAkhir = string.Format("{0:yyyyMMdd}", sPeriodeAkhir);
                    string dueDateAwal = string.Format("{0:yyyyMMdd}", sDueDateAwal);
                    string dueDateAkhir = string.Format("{0:yyyyMMdd}", sDueDateAkhir);

                    parameters += "WHERE recUserSector.UserID = ISNULL('" + base.UserID + "', recUserSector.UserID)";

                    //menngecek data balance
                    if (balance.Equals('1'))
                    {
                        parameters += " AND recBilling.Balance = recBilling.Value";
                    }
                    else if (balance.Equals('2'))
                    {
                        parameters += " AND recBilling.Balance < recBilling.Value";
                    }

                    if (!periodeAkhir.Equals(string.Empty) && !periodeAwal.Equals(string.Empty))
                    {
                        parameters += " AND recBilling.BillingDate BETWEEN ISNULL ('" + periodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + periodeAkhir + "', recBilling.BillingDate)";
                    }

                    if (!dueDateAwal.Equals(string.Empty) && !dueDateAkhir.Equals(string.Empty))
                    {
                        parameters += " AND recBilling.DueDate BETWEEN ISNULL ('" + dueDateAwal + "', recBilling.DueDate) AND ISNULL ('" + dueDateAkhir + "', recBilling.DueDate)";
                    }

                    //mengecek data transTypeID
                    if (!transTypeID.Equals(string.Empty))
                    {
                        parameters += " AND recBilling.TransTypeID = ISNULL ('" + transTypeID + "', recBilling.TransTypeID)";
                    }

                    //mengecek data debtorSectorID    
                    if (!debtorSectorID.Equals(string.Empty))
                    {
                        parameters += " AND recBilling.DebtorSectorID = ISNULL ('" + debtorSectorID + "', recBilling.DebtorSectorID)";
                    }

                    try
                    {
                        DataTable dt = recBillingDal.GetListReport(parameters);

                        rsViewer.Reset();
                        rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptBilling.rdlc");
                        ReportDataSource rds = new ReportDataSource();
                        rds.Name = "DataSet1";
                        rds.Value = dt;

                        rsViewer.LocalReport.DataSources.Clear();
                        rsViewer.LocalReport.DataSources.Add(rds);
                        rsViewer.LocalReport.Refresh();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }
}